package com.example.smartcitydemo.ui.job

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import  com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseActivity
import com.example.smartcitydemo.bean.JobListBean
import com.example.smartcitydemo.databinding.ActivityJobBinding
import com.example.smartcitydemo.databinding.ActivityJobSearchBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.viewModel.job.WorkViewModel

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.job
 * @NAME: Caleb.F
 * @DATE: 2021/11/18 14:36
 * @Email: 1919796283@qq.com
 */
class JobSearchActivity : BaseActivity<ActivityJobSearchBinding>(R.layout.activity_job_search) {
    private val jobViewModel: WorkViewModel by viewModels()
    private val jobListAdapter: JobListAdapter by lazy {
        JobListAdapter().apply {
            init()
            setDiffCallback(JobListBean.jobCallBack)
        }
    }

    override fun addObserver(contentVew: ActivityJobSearchBinding) {
        jobViewModel.jobSearchValue.observe(this) {
            Log.d(TAG, "addObserver: $it")
            when (it) {
                is Resource.Success -> {
                    Log.d(TAG, "addObserver: $it")
                    jobListAdapter.setDiffNewData(it.data!!.toMutableList())
                }
                is Resource.Failure -> {
                    Log.d(TAG, "addObserver: $it")
                    jobListAdapter.setEmptyView(R.layout.error_layout)
                }

            }
        }
    }

    override fun initData() {
        val stringExtra = intent.getStringExtra("search") ?: "java"
        jobViewModel.getJobSearchValue(stringExtra!!)
    }

    override fun initView(contentView: ActivityJobSearchBinding, savedInstanceState: Bundle?) {
        contentView.run {
            jobSearchToolbar.tvContent.text = "职位搜索"
            jobSearchRecycler.adapter = jobListAdapter

        }
    }


}